package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * Connect_Response类
 * 实现连接响应的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class Connect_Response implements AxdrType {

    private byte[] dataCode = null;
    private PIID piid_acd = null;
    private FactoryVersion fver = null;
    private Unsigned16 wver = null;
    private ProtocolConformance wpro = null;
    private FunctionConformance wfun = null;
    private Unsigned16 wSendSize = null;
    private Unsigned16 wRecvSize = null;
    private Unsigned8 wRecvWnd = null;
    private Unsigned16 wMaxApdu = null;
    private Unsigned32 wMaxIdle = null;
    private ConnectResponseInfo wConnObj = null;

    public Connect_Response() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(PIID piid_acd, FactoryVersion fver, Unsigned16 wver, ProtocolConformance wpro,
            FunctionConformance wfun, Unsigned16 wSendSize, Unsigned16 wRecvSize,
            Unsigned8 wRecvWnd, Unsigned16 wMaxApdu, Unsigned32 wMaxIdle,
            ConnectResponseInfo wConnObj) {
        this.piid_acd = piid_acd;
        this.fver = fver;
        this.wver = wver;
        this.wpro = wpro;
        this.wfun = wfun;
        this.wSendSize = wSendSize;
        this.wRecvSize = wRecvSize;
        this.wRecvWnd = wRecvWnd;
        this.wMaxApdu = wMaxApdu;
        this.wMaxIdle = wMaxIdle;
        this.wConnObj = wConnObj;
    }

    public void setPIID_ACD(PIID piid_acd) {
        this.piid_acd = piid_acd;
    }

    public void setFver(FactoryVersion fver) {
        this.fver = fver;
    }

    public void setWver(Unsigned16 wver) {
        this.wver = wver;
    }

    public void setWpro(ProtocolConformance wpro) {
        this.wpro = wpro;
    }

    public void setWfun(FunctionConformance wfun) {
        this.wfun = wfun;
    }

    public void setWSendSize(Unsigned16 wSendSize) {
        this.wSendSize = wSendSize;
    }

    public void setWRecvSize(Unsigned16 wRecvSize) {
        this.wRecvSize = wRecvSize;
    }

    public void setWRecvWnd(Unsigned8 wRecvWnd) {
        this.wRecvWnd = wRecvWnd;
    }

    public void setWMaxApdu(Unsigned16 wMaxApdu) {
        this.wMaxApdu = wMaxApdu;
    }

    public void setWMaxIdle(Unsigned32 wMaxIdle) {
        this.wMaxIdle = wMaxIdle;
    }

    public void setWConnObj(ConnectResponseInfo wConnObj) {
        this.wConnObj = wConnObj;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;

        this.piid_acd = new PIID();
        this.piid_acd.setNumBit(8);
        codeLength += this.piid_acd.decode(input);

        this.fver = new FactoryVersion();
        codeLength += this.fver.decode(input);

        this.wver = new Unsigned16();
        this.wver.setConst();
        codeLength += this.wver.decode(input);

        this.wpro = new ProtocolConformance();
        codeLength += this.wpro.decode(input);

        this.wfun = new FunctionConformance();
        codeLength += this.wfun.decode(input);

        this.wSendSize = new Unsigned16();
        this.wSendSize.setConst();
        codeLength += this.wSendSize.decode(input);

        this.wRecvSize = new Unsigned16();
        this.wRecvSize.setConst();
        codeLength += this.wRecvSize.decode(input);

        this.wRecvWnd = new Unsigned8();
        this.wRecvWnd.setConst();
        codeLength += this.wRecvWnd.decode(input);

        this.wMaxApdu = new Unsigned16();
        this.wMaxApdu.setConst();
        codeLength += this.wMaxApdu.decode(input);

        this.wMaxIdle = new Unsigned32();
        this.wMaxIdle.setConst();
        codeLength += this.wMaxIdle.decode(input);

        this.wConnObj = new ConnectResponseInfo();
        codeLength += this.wConnObj.decode(input);

        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        int codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(this.dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            if (this.wConnObj != null && this.wMaxIdle != null
                    && this.wMaxApdu != null && this.wRecvWnd != null
                    && this.wRecvSize != null && this.wSendSize != null
                    && this.wfun != null && this.wpro != null && this.wver != null
                    && this.piid_acd != null && this.fver != null) {
                codeLength = 0;
                codeLength += this.wConnObj.encode(output);
                codeLength += this.wMaxIdle.encode(output);
                codeLength += this.wMaxApdu.encode(output);
                codeLength += this.wRecvWnd.encode(output);
                codeLength += this.wRecvSize.encode(output);
                codeLength += this.wSendSize.encode(output);
                codeLength += this.wfun.encode(output);
                codeLength += this.wpro.encode(output);
                codeLength += this.wver.encode(output);
                codeLength += this.fver.encode(output);
                codeLength += this.piid_acd.encode(output);
            } else {
                throw new RuntimeException(
                        "wConnObj || wMaxIdle || wMaxApdu || wRecvWnd || wRecvSize || wSendSize || wfun || wpro || wver || fver || piid_acd is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            this.encode(revOStream);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("Error encoding Connect_Response", e);
        }
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (this.wConnObj != null && this.wMaxIdle != null
                && this.wMaxApdu != null && this.wRecvWnd != null
                && this.wRecvSize != null && this.wSendSize != null
                && this.wfun != null && this.wpro != null && this.wver != null
                && this.piid_acd != null && this.fver != null) {
            return "sequence: {" + "piid_acd: " + this.piid_acd + ", fver: " + this.fver + ", wver: " + this.wver
                    + ", wpro: " + this.wpro + ", wfun: " + this.wfun + ", wsendsize: " + this.wSendSize
                    + ", wrecvsize: " + this.wRecvSize + ", wrecvwnd: " + this.wRecvWnd + ", wmaxapdu: " + this.wMaxApdu
                    + ", wmaxidle: " + this.wMaxIdle + ", wconnobj: " + this.wConnObj + "}";
        } else {
            return "wConnObj || wMaxIdle || wMaxApdu || wRecvWnd || wRecvSize || wSendSize || wfun || wpro || wver || fver || piid_acd is null";
        }
    }

    public PIID getPiidAcd() {
        return piid_acd;
    }

    public FactoryVersion getFver() {
        return fver;
    }

    public Unsigned16 getWver() {
        return wver;
    }

    public ProtocolConformance getWpro() {
        return wpro;
    }

    public FunctionConformance getWfun() {
        return wfun;
    }

    public Unsigned16 getWSendSize() {
        return wSendSize;
    }

    public Unsigned16 getWRecvSize() {
        return wRecvSize;
    }

    public Unsigned8 getWRecvWnd() {
        return wRecvWnd;
    }

    public Unsigned16 getWMaxApdu() {
        return wMaxApdu;
    }

    public Unsigned32 getWMaxIdle() {
        return wMaxIdle;
    }

    public ConnectResponseInfo getWConnObj() {
        return wConnObj;
    }
}